<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 12-8-25
 * Time: 下午1:35
 * To change this template use File | Settings | File Templates.
 */ 
class search extends CI_Controller
{
   public function index()
   {
       $this->load->database();
       $this->db->cache_on();
       $this->db->cache_life(7200);
       $Limit = 10;
       $this->load->helper('url');
       $keyword = trim($this->input->get('keyword', true));
       $pageNo = max((int)$this->uri->rsegment(3, 1), 1);
       $offset = ($pageNo - 1) * $Limit;

       $class_id = max((int)$this->input->get('class', true), 0);
//       $searchCondition = '';
//       if($class)
//       {
//           $searchCondition = "class = {$class} AND ";
//       }
       $searchCondition = "(china_name LIKE '%{$keyword}%' OR starring LIKE '%{$keyword}%' OR director LIKE '%{$keyword}%')";

       $this->load->model('mod_category', 'category');
       $classes = $this->category->get_category();

       $query = $this->db->query("SELECT class, COUNT(id) as num FROM mp_movie WHERE {$searchCondition} GROUP BY class");
       //$totalNum = $this->movie->getMovieCount($searchCondition);
       $totalDetail  = $query->result_array('class');
       //print_r($totalDetail);
       $totalNum = 0;


       foreach ($totalDetail as $k => $count) {
           $totalDetail[$k]['name'] = isset($classes[$count['class']]) ? $classes[$count['class']]['name'] : '其他';
           $totalNum += $count['num'];
       }
       $_totalNum = $totalNum;
       //print_r($totalDetail);
       if (isset($totalDetail[$class_id])) {
           $searchCondition = "class = {$class_id} AND " . $searchCondition;
           $_totalNum = $totalDetail[$class_id]['num'];
       }

       $categoryData = array();
       $this->load->model('movie');
       if($_totalNum > 0)
       {
           $field = 'id, china_name, director, starring, area, type, version, language, img_addr, public_time, drama, create_time';
           $categoryData  = $this->movie->getMovieList($field, $Limit, $offset, $searchCondition, 'update_time desc');
       }
       $pageHtml = '';
       if($_totalNum > $Limit)
       {
           $this->load->library('pagination');
           $config['base_url'] = config_item('i_base_url') . '/search';
           $config['suffix'] = '?keyword='.$keyword;
           $config['total_rows'] = $_totalNum;
           $config['per_page'] = $Limit;
           $config['num_links'] = 4;
           $config['uri_segment'] = 3;
           $config['first_link'] = '首页';
           $config['last_link'] = '尾页';
           $config['use_page_numbers'] = TRUE;
           $config['cur_tag_open'] = '<a id="cur">';
           $config['cur_tag_close'] = '</a>';
           $config['prev_link'] = '上一页';
           $config['next_link'] = '下一页';
           $this->pagination->initialize($config);
           $pageHtml = $this->pagination->create_links();
       }
       $field = 'id, china_name, area, type, version, language, img_addr,drama, create_time';
       $tuijian = $this->movie->getMovieList($field, 10, 0, NULL, 'update_time desc');//'1' => '电影'

       $vData = array(
           'title' => $keyword." 搜索结果页 电影1010",
           'keyword' => $keyword,
           'totalDetail'=> $totalDetail,
           'data'  => $categoryData,
           'page_html' => $pageHtml,
           'totalNum' => $totalNum,
           '_totalNum' => $_totalNum,
           'tuijian' => $tuijian,
           'class_id' => $class_id,
           "keywords" => '提供全网最全资源库 迅雷下载 电驴下载 电影1010',
           'description' => "电影1010 提供全网最全的电影,剧集,音乐,综艺,动漫等资源库, 在线观看 迅雷下载 电驴下载 剧情介绍",
       );

       //echo '<pre>';print_r($vData);
       $this->load->view('search', $vData);
       $this->db->insert('search_log', array('keyword'=>$keyword,'search_type'=>$class_id, 'ip'=>$this->input->ip_address(), 'create_time' => date('Y-m-d H:i:s')));
   }
}
